﻿// SilverlightDesktop.Net - http://www.SilverlightDesktop.net
// Copyright (c) 2008
// by SilverlightDesktop.Net
//
// DotNetNuke® - http://www.dotnetnuke.com
// Copyright (c) 2002-2008
// by DotNetNuke Corporation
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.

using System;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Net.Mail;
using System.Web.Mail;
using System.Text;
using System.Collections.Generic;
using System.IO;

public class Utility
{
    public Utility()
    {
    }

    #region ConvertToText
    public static string ConvertToText(string sHTML)
    {
        string sContent = sHTML;
        sContent = sContent.Replace("<br />", Environment.NewLine);
        sContent = sContent.Replace("<br>", Environment.NewLine);
        sContent = FormatText(sContent, true);
        return StripTags(sContent, true);
    }
    #endregion

    #region FormatText
    public static string FormatText(string HTML, bool RetainSpace)
    {
        //Match all variants of <br> tag (<br>, <BR>, <br/>, including embedded space
        string brMatch = "\\s*<\\s*[bB][rR]\\s*/\\s*>\\s*";
        //Replace Tags by replacement String and return mofified string
        return System.Text.RegularExpressions.Regex.Replace(HTML, brMatch, Environment.NewLine);
    }
    #endregion

    #region StripTags
    public static string StripTags(string HTML, bool RetainSpace)
    {
        //Set up Replacement String
        string RepString;
        if (RetainSpace)
        {
            RepString = " ";
        }
        else
        {
            RepString = "";
        }

        //Replace Tags by replacement String and return mofified string
        return System.Text.RegularExpressions.Regex.Replace(HTML, "<[^>]*>", RepString);
    }
    #endregion    

    #region GetAllFileNames
    /// <summary>
    /// Find all files in a directory, and all files within every nested
    /// directory.
    /// from: http://dotnetperls.com/Content/Recursively-Find-Files.aspx
    /// </summary>
    /// <param name="baseDir">The starting directory you want to use.</param>
    /// <returns>A string array containing all the file names.</returns>
    public static string[] GetAllFileNames(string baseDir)
    {
        // Store results in the file results list.
        List<string> fileResults = new List<string>();

        // Store a stack of our directories.
        Stack<string> directoryStack = new Stack<string>();
        directoryStack.Push(baseDir);

        // While there are directories to process and we don't have too many results
        while (directoryStack.Count > 0 && fileResults.Count < 1000)
        {
            string currentDir = directoryStack.Pop();

            // Add all files at this directory.
            foreach (string fileName in Directory.GetFiles(currentDir, "*.*"))
            {
                fileResults.Add(fileName);
            }

            // Add all directories at this directory.
            foreach (string directoryName in Directory.GetDirectories(currentDir))
            {
                directoryStack.Push(directoryName);
            }
        }
        return fileResults.ToArray();
    } 
    #endregion

    #region GetRandomPassword
    public string GetRandomPassword()
    {
        StringBuilder builder = new StringBuilder();
        Random random = new Random();
        int intElements = random.Next(10, 26);

        for (int i = 0; i < intElements; i++)
        {
            int intRandomType = random.Next(0, 2);
            if (intRandomType == 1)
            {
                char ch;
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            else
            {
                builder.Append(random.Next(0, 9));
            }
        }
        return builder.ToString();
    } 
    #endregion

}


